//
//  SGSHelpViewController.m
//  AgileNYC
//
//  Created by German on 04.08.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "SGSHelpViewController.h"
#import "SGSModelsManager.h"
#import "SGSDelegatesManager.h"
#import "SGSDialogsManager.h"
#import "SGSUtils.h"
#import "SGSAppConsts.h"

@interface SGSHelpViewController ()

@property NSInteger currentHelpPage;

@end

@implementation SGSHelpViewController

- (NSUInteger)supportedInterfaceOrientations {
    return  UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.pnlNavigation.hidden = YES;

    UISwipeGestureRecognizer * swipeLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    UISwipeGestureRecognizer * swipeRightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];

    swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:swipeLeftRecognizer];
    [self.view addGestureRecognizer:swipeRightRecognizer];
    
    //self.vwLookView.backgroundColor = [UIColor whiteColor];
    
    self.vwPage1Indicator.layer.cornerRadius = self.vwPage1Indicator.frame.size.height/2;
    self.vwPage1Indicator.layer.masksToBounds = YES;
    
    self.vwPage2Indicator.layer.cornerRadius = self.vwPage2Indicator.frame.size.height/2;
    self.vwPage2Indicator.layer.masksToBounds = YES;

    self.vwPage3Indicator.layer.cornerRadius = self.vwPage3Indicator.frame.size.height/2;
    self.vwPage3Indicator.layer.masksToBounds = YES;
    
    GSLog(GSLogInfo, @"Parent View: %@", [NSValue valueWithCGRect:self.vwLookView.frame]);
    
    self.imgPage1.frame = CGRectMake(0,
                                     0,
                                     self.vwLookView.frame.size.width,
                                     self.vwLookView.frame.size.height);

    GSLog(GSLogInfo, @"Page1 : %@", [NSValue valueWithCGRect:self.imgPage1.frame]);

    self.imgPage2.frame = CGRectMake(0,
                                     0,
                                     self.vwLookView.frame.size.width,
                                     self.vwLookView.frame.size.height);

    self.imgPage3.frame = CGRectMake(0,
                                     0,
                                     self.vwLookView.frame.size.width,
                                     self.vwLookView.frame.size.height);

    self.currentHelpPage = 1;
    [self loadCurrentPage:NO];
    

}

- (void)viewDidAppear:(BOOL)animated {
   [self loadSessionID];
}

#pragma mark - Actions

-(void) loadSessionID {
    NSString * sessionID = [[SGSModelsManager sharedModelsManager] currentUser].sessionID;
    
    if ((sessionID) && (![sessionID isEqualToString:@"0"]) && (![sessionID isEqualToString:@"(null)"])) {
        self.labSessionId.text = sessionID;
        self.labSessionId.hidden = NO;
    } else {
        self.labSessionId.hidden = YES;
    }
}

- (IBAction)onBackButtonClick:(UIButton *)sender {
    /*SGSDelegatesManager * delegatesManager = [SGSDelegatesManager sharedDelegatesManager];
    
    if (delegatesManager.currentEventsVC) {
        [self.navigationController popToViewController:delegatesManager.currentEventsVC animated:YES];
    } else {
        UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SGSEventsViewController"];
        
        [self.navigationController pushViewController:vc animated:YES];
    }*/
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)onConnectButtonClick:(UIButton *)sender {
    //[self checkLoginAndMakeConnection];
    
    [[SGSDelegatesManager sharedDelegatesManager].currentEventsVC showConnectionDialog];
}

-(void) checkLoginAndMakeConnection {
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    if (![modelsManager checkUserLogin]) {
        
        UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SGSLoginViewController"];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
        [[SGSDialogsManager dialogsManager] showConnectionDialog:@""
              onComplete:^(NSInteger buttonIndex, NSString *returnText) {
                  GSLog(GSLogInfo, @"[SGSHelpViewController] Connection dialog. Button pressed: %d. Returned: %@", buttonIndex, returnText);
                  
                  switch (buttonIndex) {
                      case 0: // Connect or Accept
                          [[SGSModelsManager sharedModelsManager] sendConnectionRequest:returnText onComplete:^(BOOL result, NSError * error) {
                              if (!result) {
                                  [[SGSDialogsManager dialogsManager] showSimpleDialog:Error_Dialog_Title message:[error domain] onComplete:nil];
                              }
                          }];
                          break;
                      case 1: //Cancel or Decline
                          GSLog(GSLogWarning, @"[SGSHelpViewController] Connection cancelled");
                          break;
                      default: GSLog(GSLogWarning, @"[SGSHelpViewController] Unknown button pressed");
                  }
                  
              }];
    }
}

#pragma mark - UIGestureRecognizer

-(void) handleLeftSwipe: (UIGestureRecognizer*) sender {
    if (self.currentHelpPage<3) {
        self.currentHelpPage++;
        [self loadCurrentPage:YES];
    }

}

-(void) handleRightSwipe: (UIGestureRecognizer*) sender {
    if (self.currentHelpPage>1) {
        self.currentHelpPage--;
        [self loadCurrentPage:YES];
    }
}

- (IBAction)onSwipeRightButtonClick:(UIButton *)sender {
    [self handleLeftSwipe:nil];
}

- (IBAction)onSwipeLeftButtonClick:(UIButton *)sender {
    [self handleRightSwipe:nil];
}

#pragma mark - Page Changes

-(void) loadCurrentPage:(BOOL) animation {
    CGFloat animDelay = 0.0f;
    
    if (animation) {
        animDelay = 0.2f;
    }
    
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
    if (self.currentHelpPage==1) {
            self.imgPage1.hidden = NO;
        
            [UIView animateWithDuration:animDelay
                                  delay:0
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                 self.imgPage1.frame = CGRectMake(0,
                                                                  self.imgPage1.frame.origin.y,
                                                                  self.imgPage1.frame.size.width,
                                                                  self.imgPage1.frame.size.height);
                                 self.imgPage2.frame = CGRectMake(self.imgPage1.frame.origin.x + self.imgPage1.frame.size.width,
                                                                  self.imgPage2.frame.origin.y,
                                                                  self.imgPage2.frame.size.width,
                                                                  self.imgPage2.frame.size.height);
                                 self.imgPage3.frame = CGRectMake(self.imgPage2.frame.origin.x + self.imgPage2.frame.size.width,
                                                                  self.imgPage3.frame.origin.y,
                                                                  self.imgPage3.frame.size.width,
                                                                  self.imgPage3.frame.size.height);
                                 self.vwPage1Indicator.backgroundColor = COLOR_SEC_BLUE;
                                 self.vwPage2Indicator.backgroundColor = [UIColor lightGrayColor];
                                 self.vwPage3Indicator.backgroundColor = [UIColor lightGrayColor];
                             }
                             completion:^(BOOL finished){
                                 self.imgPage2.hidden = YES;
                                 self.imgPage3.hidden = YES;
                                 [[UIApplication sharedApplication] endIgnoringInteractionEvents];
                             }];
    } else
    if (self.currentHelpPage==2) {
            self.imgPage2.hidden = NO;

            [UIView animateWithDuration:animDelay
                                  delay:0
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                 self.imgPage1.frame = CGRectMake(self.imgPage1.frame.size.width*-1,
                                                                  self.imgPage1.frame.origin.y,
                                                                  self.imgPage1.frame.size.width,
                                                                  self.imgPage1.frame.size.height);
                                 self.imgPage2.frame = CGRectMake(0,
                                                                  self.imgPage2.frame.origin.y,
                                                                  self.imgPage2.frame.size.width,
                                                                  self.imgPage2.frame.size.height);
                                 self.imgPage3.frame = CGRectMake(self.imgPage2.frame.origin.x + self.imgPage2.frame.size.width,
                                                                  self.imgPage3.frame.origin.y,
                                                                  self.imgPage3.frame.size.width,
                                                                  self.imgPage3.frame.size.height);
                                 self.vwPage1Indicator.backgroundColor = [UIColor lightGrayColor];
                                 self.vwPage2Indicator.backgroundColor = COLOR_SEC_BLUE;
                                 self.vwPage3Indicator.backgroundColor = [UIColor lightGrayColor];
                             }
                             completion:^(BOOL finished){
                                 self.imgPage1.hidden = YES;
                                 self.imgPage3.hidden = YES;
                                 [[UIApplication sharedApplication] endIgnoringInteractionEvents];
                             }];
    } else
    if (self.currentHelpPage==3) {
        self.imgPage3.hidden = NO;

        [UIView animateWithDuration:animDelay
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.imgPage1.frame = CGRectMake((self.imgPage1.frame.size.width + self.imgPage2.frame.size.width)*-1,
                                                              self.imgPage1.frame.origin.y,
                                                              self.imgPage1.frame.size.width,
                                                              self.imgPage1.frame.size.height);
                             self.imgPage2.frame = CGRectMake(self.imgPage2.frame.size.width * -1,
                                                              self.imgPage2.frame.origin.y,
                                                              self.imgPage2.frame.size.width,
                                                              self.imgPage2.frame.size.height);
                             self.imgPage3.frame = CGRectMake(0,
                                                              self.imgPage3.frame.origin.y,
                                                              self.imgPage3.frame.size.width,
                                                              self.imgPage3.frame.size.height);
                             self.vwPage1Indicator.backgroundColor = [UIColor lightGrayColor];
                             self.vwPage2Indicator.backgroundColor = [UIColor lightGrayColor];
                             self.vwPage3Indicator.backgroundColor = COLOR_SEC_BLUE;
                         }
                         completion:^(BOOL finished){
                             self.imgPage1.hidden = YES;
                             self.imgPage2.hidden = YES;
                             [[UIApplication sharedApplication] endIgnoringInteractionEvents];
                         }];
    } else {
       [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }

}

#pragma mark - System Functions

- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}



@end
